package com.fms.trollalarm.fragment;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.fms.trollalarm.R;
import com.fms.trollalarm.utils.ShakeDetector;
import com.fms.trollalarm.utils.ShakeDetector.OnShakeListener;

public class NotTrollFragment extends AlarmFragment {
	private Button btDimiss;
	private Button btSnooze;
	private SensorManager mSensorManager;
	private Sensor mAccelerometer;
	private ShakeDetector mShakeDetector;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_not_troll, null);
		findViewIDs(v);
		init();
		setEventView();
		return v;
	}

	private void findViewIDs(View v) {
		btDimiss = (Button) v.findViewById(R.id.bt_dimiss);
		btSnooze = (Button) v.findViewById(R.id.bt_snooze);
	}

	private void init() {
		mSensorManager = (SensorManager) getActivity().getSystemService(
				Context.SENSOR_SERVICE);
		mAccelerometer = mSensorManager
				.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
		mShakeDetector = new ShakeDetector();

	}

	private void setEventView() {
		btDimiss.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setOnStopAlarm();
			}
		});

		btSnooze.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				setOnSnoozeAlarm();
			}
		});

		mShakeDetector.setOnShakeListener(new OnShakeListener() {

			@Override
			public void onShake(int count) {
				setOnStopAlarm();
			}
		});
	}

	@Override
	public void onResume() {
		super.onResume();
		mSensorManager.registerListener(mShakeDetector, mAccelerometer,
				SensorManager.SENSOR_DELAY_UI);

	}

	@Override
	public void onPause() {
		mSensorManager.unregisterListener(mShakeDetector);
		super.onPause();
	}
}
